//
//  HESearchResultController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/17.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HESearchResultController.h"
#import "HESearchGoodsCell.h"
#import "HESearchHealthCell.h"
#import "HESearchPageCell.h"
#import "HEGoodsDetailsViewController.h"
#import "HEEduVideoDetailsController.h"

#import "HEBusinessCardViewController.h"
#import "HEFreeConsulViewController.h"
#import "HEHealthManageViewController.h"
#import "HEHealthCheckDataController.h"
#import "HEConsulInfoViewController.h"
#import "HESystemInfoViewController.h"
#import "HEOrderViewController.h"
#import "HEPersonCenterViewController.h"
#import "HEBusinessCardViewController.h"
#import "ZFScanViewController.h"
#import "HEHealthNoticeViewController.h"
#import "HESecurityViewController.h"
#import "IMSetFontSizeViewController.h"
#import "HEAddressViewController.h"
#import "HEHealthEduViewController.h"
#import "HEReportReadViewController.h"
#import "HEHealthRecordViewController.h"
#import "HEMyCollectViewController.h"
#import "HEAfterListViewController.h"
#import "HEHealthAssesViewController.h"
#import "HEFindDoctorViewController.h"
#import "HEFindTeamViewController.h"
#import "HEServerOrgViewController.h"

@interface HESearchResultController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) GDFormModel *formModel;
@property (nonatomic, strong) NSMutableArray *sections;
@property (nonatomic, strong) NSMutableArray <HEGroupSearchModel *> *dataSource;
@property (nonatomic, strong) NSString *drugsUrl;


@end

@implementation HESearchResultController

- (GDFormModel *)formModel {
    if (!_formModel) {
        _formModel = [[GDFormModel alloc] init];
        _formModel.proxyVC = self;
    }
    return _formModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"搜索结果";

    [self initUI];
    
    [self setCustomTableView:self.tableView];
    [self showEmpty];
    [self.view addSubview:self.tableView];
}

- (void)initUI {
   _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, 10, KScreenWidth, KScreenHeight - ZXNavBarHeight - 10) style:UITableViewStylePlain];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];
    _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];
   _tableView.separatorColor = CSplitLineColor;
   _tableView.scrollsToTop = YES;
   _tableView.tableFooterView = [[UIView alloc] init];
   
   self.tableView.backgroundColor = KWhiteColor;
   self.tableView.delegate = self;
   self.tableView.dataSource = self;
   [self.view addSubview:self.tableView];
}

- (void)initCell {
   // 总段
   _sections = [NSMutableArray array];
   NSMutableArray *classArr = [NSMutableArray array];
  
    GDFiterCellModel *goodsModel = [GDFiterCellModel new];
    goodsModel.cellKlass = HESearchGoodsCell.class;
    
    GDFiterCellModel *menModel = [GDFiterCellModel new];
    menModel.cellKlass = HESearchPageCell.class;
    
    GDFiterCellModel *infoModel = [GDFiterCellModel new];
    infoModel.cellKlass = HESearchHealthCell.class;
 
    classArr = @[goodsModel, menModel, infoModel].mutableCopy;

   // 添加的时候注意顺序，暂未对顺序做处理
   [self.tableView mc_registerCellByFiterModels: classArr];
    
    for (HEGroupSearchModel *group in _dataSource) {
        NSMutableArray *sec = [NSMutableArray array];
        for (HESearchDataItemModel *item in group.childs) {
            GDFiterCellModel *infoModel = [GDFiterCellModel new];
            infoModel.cellId = group.cellId;
            [sec addObject:infoModel];
        }
        [_sections addObject:sec];
    }
    
    [_tableView reloadData];
}

- (void)refreshData:(NSMutableArray <HEGroupSearchModel *>*)data {
    _dataSource = data;
    [self initCell];
    
    if (_dataSource.count == 0) {
        [self showEmpty];
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return _sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   NSMutableArray *sec = _sections[section];
   return sec.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *sec = _sections[indexPath.section];
    GDFiterCellModel *fiterModel = sec[indexPath.row];

    HEGroupSearchModel *group = _dataSource[indexPath.section];
    HESearchDataItemModel *item = group.childs[indexPath.row];
    
    MCTableBaseCell *cell = (MCTableBaseCell *)[_tableView dequeueReusableCellWithIdentifier:fiterModel.cellId];
    [cell configCellWithModel: item];
    
   return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  NSMutableArray *sec = _sections[indexPath.section];
   GDFiterCellModel *fiterModel = sec[indexPath.row];
    
    HEGroupSearchModel *group = _dataSource[indexPath.section];
    HESearchDataItemModel *item = group.childs[indexPath.row];

    return [tableView fd_heightForCellWithIdentifier: fiterModel.cellId cacheByIndexPath:indexPath configuration:^(MCTableBaseCell *cell) {
        [cell configCellWithModel: item];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 
    HEGroupSearchModel *group = _dataSource[indexPath.section];
    HESearchDataItemModel *item = group.childs[indexPath.row];
    
    if (group.type == HEGroupSearchContentTypeGoods) {
        HEGoodsDetailsViewController *vc = [HEGoodsDetailsViewController new];
        vc.goodsId = [item.id integerValue];
        [self.navigationController pushViewController:vc animated:YES];
    }else if(group.type == HEGroupSearchContentTypeHealthInfo) {
        NSURL * file = [NSURL URLWithString: [NSString stringWithFormat:@"%@/details/#/text?id=%@", KServerUrl, item.id]]; 
        RootWebViewController *webView = [[RootWebViewController alloc] initWithUrl:file.absoluteString];
        webView.isShowCloseBtn = YES;
        webView.title = @"资讯详请";
        webView.showPageTitle = NO;
        webView.isShowShare = YES;
        
        HEMissonListModel *infoModel = [HEMissonListModel new];
        infoModel.img_url = item.img_url;
        infoModel.id = item.id.integerValue;
        infoModel.info_title = item.title;
        infoModel.information_brief = item.sub_title;
        webView.model = infoModel;
        [self.navigationController pushViewController:webView animated:YES];
    }else if(group.type == HEGroupSearchContentTypeVideo) {
        HEEduVideoDetailsController * vc = [HEEduVideoDetailsController new];
        vc.infoId = [item.id integerValue];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (group.type == HEGroupSearchContentTypeMenu) {
        [self systemPageJump: item];
    }
}


#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    NSMutableArray *sec = _sections[section];
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    HEGroupSearchModel *group = _dataSource[section];

    UIView *v = [[UIView alloc] init];
    v.backgroundColor = CViewBgColor;
    
    UILabel *titleLb = [UILabel new];
    titleLb.text = group.groupName;
    titleLb.font = DYFont(16);
    titleLb.textColor = CNavLightBlackColor;
    [v addSubview:titleLb];
    
    [titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(v);
        make.left.equalTo(v).offset(15);
        make.right.equalTo(v).offset(-15);
    }];
    
    return v;
}

- (void)systemPageJump:(HESearchDataItemModel *)page {
   
    if ([@"二维码名片" isEqualToString: page.title]) {
        HEBusinessCardViewController *vc = [HEBusinessCardViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"免费咨询" isEqualToString: page.title]) {
        HEFreeConsulViewController *vc = [HEFreeConsulViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"健康管理" isEqualToString: page.title]) {
        HEHealthManageViewController *vc = [HEHealthManageViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"身高" isEqualToString: page.title]) {
        HEHealthCheckDataController *vc = [HEHealthCheckDataController new];
        vc.index = 0;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"体重" isEqualToString: page.title]) {
        HEHealthCheckDataController *vc = [HEHealthCheckDataController new];
        vc.index = 1;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"体温" isEqualToString: page.title]) {
        HEHealthCheckDataController *vc = [HEHealthCheckDataController new];
        vc.index = 2;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"呼吸次数" isEqualToString: page.title]) {
        HEHealthCheckDataController *vc = [HEHealthCheckDataController new];
        vc.index = 3;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"睡眠" isEqualToString: page.title]) {
        HEHealthCheckDataController *vc = [HEHealthCheckDataController new];
        vc.index = 4;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"血压" isEqualToString: page.title]) {
        HEHealthCheckDataController *vc = [HEHealthCheckDataController new];
        vc.index = 5;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"血糖" isEqualToString: page.title]) {
        HEHealthCheckDataController *vc = [HEHealthCheckDataController new];
        vc.index = 6;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"血氧" isEqualToString: page.title]) {
        HEHealthCheckDataController *vc = [HEHealthCheckDataController new];
        vc.index = 7;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"静息心率" isEqualToString: page.title]) {
        HEHealthCheckDataController *vc = [HEHealthCheckDataController new];
        vc.index = 8;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"BMI" isEqualToString: page.title]) {
        HEHealthCheckDataController *vc = [HEHealthCheckDataController new];
        vc.index = 9;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"问诊消息" isEqualToString: page.title]) {
        HEConsulInfoViewController *vc = [HEConsulInfoViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"系统消息" isEqualToString: page.title]) {
        HESystemInfoViewController *vc = [HESystemInfoViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"订单" isEqualToString: page.title]) {
        HEOrderViewController *vc = [HEOrderViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"个人信息" isEqualToString: page.title]) {
        HEPersonCenterViewController *vc = [HEPersonCenterViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"二维码" isEqualToString: page.title]) {
        ZFScanViewController *vc = [ZFScanViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"二维码名片" isEqualToString: page.title]) {
        HEBusinessCardViewController *vc = [HEBusinessCardViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"健康提醒" isEqualToString: page.title]) {
        HEHealthNoticeViewController *vc = [HEHealthNoticeViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"安全设置" isEqualToString: page.title]) {
        HESecurityViewController *vc = [HESecurityViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"字体设置" isEqualToString: page.title]) {
        IMSetFontSizeViewController *vc = [IMSetFontSizeViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"收货地址" isEqualToString: page.title]) {
        HEAddressViewController *vc = [HEAddressViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"医药上门" isEqualToString: page.title]) {
        if (!self.drugsUrl) {
            [self loadDrugsUrl];
        }else {
            NSURL * file = [NSURL URLWithString:_drugsUrl];
         
            RootWebViewController *webView = [[RootWebViewController alloc] initWithUrl:file.absoluteString];
            webView.isShowCloseBtn = YES;
             webView.title = @"医药上门";
             webView.showPageTitle = NO;
             webView.header = @{@"Referer":@"https://pay.miaoshou.com"}.mutableCopy;
            [self.navigationController pushViewController:webView animated:YES];
        }
    }
    else if ([@"健康资讯" isEqualToString: page.title]) {
        HEHealthEduViewController *vc = [HEHealthEduViewController new];
        vc.index = 0;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"健康视频" isEqualToString: page.title]) {
        HEHealthEduViewController *vc = [HEHealthEduViewController new];
        vc.index = 1;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"报告解读" isEqualToString: page.title]) {
        HEReportReadViewController *vc = [HEReportReadViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"健康档案" isEqualToString: page.title]) {
        HEHealthRecordViewController *vc = [HEHealthRecordViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"我的收藏" isEqualToString: page.title]) {
        HEMyCollectViewController *vc = [HEMyCollectViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"我的订单" isEqualToString: page.title]) {
        HEOrderViewController *vc = [HEOrderViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"售后列表" isEqualToString: page.title] ||
             [@"申请售后" isEqualToString: page.title]) {
        HEAfterListViewController *vc = [HEAfterListViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"健康评估" isEqualToString: page.title]) {
        HEHealthAssesViewController *vc = [HEHealthAssesViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"找医生" isEqualToString: page.title]) {
        HEServerOrgViewController *vc = [HEServerOrgViewController new];
        vc.type = HEServerOrgTypeDoctor;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([@"找团队" isEqualToString: page.title]) {
        HEServerOrgViewController *vc = [HEServerOrgViewController new];
        vc.type = HEServerOrgTypeTeam;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


- (void)loadDrugsUrl {
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KdrugsUrl parameters:@{} success:^(id  _Nonnull responseObject) {
        HEObjectDataResponse *response = [HEObjectDataResponse mj_objectWithKeyValues:responseObject];
        self.drugsUrl = response.data;
        
        if (response.success) {
            NSURL * file = [NSURL URLWithString:self.drugsUrl];
            RootWebViewController *webView = [[RootWebViewController alloc] initWithUrl:file.absoluteString];
            webView.isShowCloseBtn = YES;
            webView.title = @"医药上门";
            webView.showPageTitle = NO;
            webView.header = @{@"Referer":@"https://pay.miaoshou.com"}.mutableCopy;
            [self.navigationController pushViewController:webView animated:YES];
        }
    } failure:^(NSError * _Nonnull error) {
    }];
}

@end
 
